package br.ufmg.dees.insane.ui.rich.prep.view.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import br.ufmg.dees.insane.ui.rich.prep.view.InterfacePrep;

public class MoveNodeOrFrameDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel panelIncrements = null;

	private JLabel lblDeltaX = null;
	private JLabel lblDeltaY = null;
	private JLabel lblDeltaZ = null;
	
	private JTextField txtfDeltaX = null;
	private JTextField txtfDeltaY = null;
	private JTextField txtfDeltaZ = null;
	
	private JPanel panelButtons = null;
	private JPanel panelMain1 = null;
	
	private JButton btnOk = null;
	private JButton btnCancel = null;
	
	private double deltaX, deltaY, deltaZ;
	
	private InterfacePrep intFrame;

	/**
	 * This is the default constructor
	 */
	public MoveNodeOrFrameDialog(String title, InterfacePrep parent) 
	{
		this.setTitle(title);
		this.intFrame = parent;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setResizable(false);
		this.setModal(true);
		this.setPreferredSize(new Dimension(190, 180));
		this.setBounds(new Rectangle(0, 0, 190, 180));
		this.setName("dlgMove");
		this.setMinimumSize(new Dimension(190, 180));
		this.setMaximumSize(new Dimension(190, 180));
		this.setContentPane(getPanelMain1());
		this.setLocationRelativeTo(intFrame);
		this.validate();
		this.setVisible(true);
	}

	/**
	 * This method initializes panelIncrements
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelIncrements() {
		if (panelIncrements == null) {
			lblDeltaZ = new JLabel();
			lblDeltaZ.setText("delta Z");
			lblDeltaZ.setHorizontalAlignment(SwingConstants.CENTER);
			lblDeltaZ.setPreferredSize(new Dimension(30, 30));
			lblDeltaY = new JLabel();
			lblDeltaY.setText("delta Y");
			lblDeltaY.setHorizontalAlignment(SwingConstants.CENTER);
			lblDeltaY.setPreferredSize(new Dimension(30, 30));
			lblDeltaX = new JLabel();
			lblDeltaX.setText("delta X");
			lblDeltaX.setHorizontalAlignment(SwingConstants.CENTER);
			lblDeltaX.setPreferredSize(new Dimension(30, 30));
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(3);
			gridLayout.setHgap(5);
			gridLayout.setVgap(5);
			gridLayout.setColumns(2);
			panelIncrements = new JPanel();
			panelIncrements.setBorder(BorderFactory.createTitledBorder(null, "Increments", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelIncrements.setPreferredSize(new Dimension(190, 120));
			panelIncrements.setLayout(gridLayout);
			panelIncrements.add(lblDeltaX, null);
			panelIncrements.add(getTxtfDeltaX(), null);
			panelIncrements.add(lblDeltaY, null);
			panelIncrements.add(getTxtfDeltaY(), null);
			panelIncrements.add(lblDeltaZ, null);
			panelIncrements.add(getTxtfDeltaZ(), null);
		}
		return panelIncrements;
	}

	/**
	 * This method initializes txtfDeltaX	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtfDeltaX() {
		if (txtfDeltaX == null) {
			txtfDeltaX = new JTextField();
			txtfDeltaX.setText("0.0");
			txtfDeltaX.setHorizontalAlignment(JTextField.CENTER);
			txtfDeltaX.setPreferredSize(new Dimension(30, 30));
		}
		return txtfDeltaX;
	}

	/**
	 * This method initializes txtfDeltaY	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtfDeltaY() {
		if (txtfDeltaY == null) {
			txtfDeltaY = new JTextField();
			txtfDeltaY.setText("0.0");
			txtfDeltaY.setHorizontalAlignment(JTextField.CENTER);
			txtfDeltaY.setPreferredSize(new Dimension(30, 30));
		}
		return txtfDeltaY;
	}

	/**
	 * This method initializes txtfDeltaZ	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtfDeltaZ() {
		if (txtfDeltaZ == null) {
			txtfDeltaZ = new JTextField();
			txtfDeltaZ.setText("0.0");
			txtfDeltaZ.setHorizontalAlignment(JTextField.CENTER);
			txtfDeltaZ.setPreferredSize(new Dimension(30, 30));
		}
		return txtfDeltaZ;
	}

	/**
	 * This method initializes panelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelButtons() {
		if (panelButtons == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(10);
			panelButtons = new JPanel();
			panelButtons.setLayout(flowLayout);
			panelButtons.setPreferredSize(new Dimension(190, 35));
			panelButtons.add(getBtnOk(), null);
			panelButtons.add(getBtnCancel(), null);
		}
		return panelButtons;
	}

	/**
	 * This method initializes panelMain1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelMain1() {
		if (panelMain1 == null) {
			panelMain1 = new JPanel();
			panelMain1.setLayout(new BorderLayout());
			panelMain1.add(getPanelIncrements(), BorderLayout.CENTER);
			panelMain1.add(getPanelButtons(), BorderLayout.SOUTH);
		}
		return panelMain1;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setPreferredSize(new Dimension(80, 25));
			btnOk.setText("Ok");
			btnOk.addActionListener(this);
	        getRootPane().setDefaultButton(btnOk);
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setPreferredSize(new Dimension(80, 25));
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(this);
		}
		return btnCancel;
	}
	
//  ****************************************************************************	
    
	public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == btnOk)
        {
    		try 
    		{
    			deltaX = Double.valueOf(this.txtfDeltaX.getText());
    			deltaY = Double.valueOf(this.txtfDeltaY.getText());
    			deltaZ = Double.valueOf(this.txtfDeltaZ.getText());
    			this.setVisible(false);
    		}
    		catch (Exception exc) 
    		{
    			String msg = "Some value is not valid. Verify !";
    			JOptionPane.showMessageDialog(this, msg,
    					"Alert Message", JOptionPane.INFORMATION_MESSAGE);
    			txtfDeltaX.requestFocus();
    		}
        }
        else
        {
        	this.setVisible(false);
        }
    }	
	
//  ****************************************************************************	
	
	public double getDeltaX()
	{
		return this.deltaX;
	}	
	
//  ****************************************************************************
	
	public double getDeltaY()
	{
		return this.deltaY;
	}	
	
//  ****************************************************************************
	
	public double getDeltaZ()
	{
		return this.deltaZ;
	}	

//  ****************************************************************************
}  //  @jve:decl-index=0:visual-constraint="43,21"
